package org.gp.Interfaz;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JDialog;

import org.gp.tringes.GestorIGAnadirCamada;
import org.gp.tringes.GestorIGInicio;

import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.GridLayout;
import javax.swing.JTextField;
import java.awt.FlowLayout;
import javax.swing.JButton;
import javax.swing.SwingConstants;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Locale;


public class IGAnadirCamada extends JDialog{
	
	private String numPerro;
	
	private JPanel panel;
	private JPanel panel_1;
	private JPanel panel_2;
	private JLabel lblFecNac;
	private JTextField txtFecNac;
	private JLabel lblInvisible;
	private JLabel lblFormato;
	private JLabel lblNumCrias;
	private JTextField txtNumCrias;
	private JButton btnAnadir;
	private JButton btnSalir;
	
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					IGAnadirCamada dialog = new IGAnadirCamada("");
					dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
					dialog.setVisible(true);
					dialog.setModal(true);
					dialog.setResizable(false);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	
	public IGAnadirCamada(String pNumPerro){
		numPerro = pNumPerro;
		
		setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
		//ID-16
		setTitle(GestorIGInicio.getGestorIGInicio().getTexto(16));
		setBounds(100, 100, 350, 200);
		
		getContentPane().setLayout(new BorderLayout(0, 0));
		getContentPane().add(getPanel(), BorderLayout.NORTH);
		getContentPane().add(getPanel_1(), BorderLayout.CENTER);
		getContentPane().add(getPanel_2(), BorderLayout.SOUTH);
		
		
	}
	
	private JPanel getPanel(){
		if(panel == null){
			panel = new JPanel();
		}
		return panel;
	}
	
	private JPanel getPanel_1(){
		if(panel_1 == null){
			panel_1 = new JPanel();
			panel_1.setLayout(new GridLayout(3, 2, 0, 0));
			panel_1.add(getLblFecNac());
			panel_1.add(getTxtFecNac());
			panel_1.add(getLblInvisible());
			panel_1.add(getLblFormato());
			panel_1.add(getLblNumCrias());
			panel_1.add(getTxtNumCrias());
		}
		return panel_1;
	}
	
	private JPanel getPanel_2(){
		if(panel_2 == null){
			panel_2 = new JPanel();
			panel_2.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
			panel_2.add(getBtnAnadir());
			panel_2.add(getBtnSalir());
		}
		return panel_2;
	}
	
	private JLabel getLblFecNac() {
		if (lblFecNac == null) {
			//ID-17
			lblFecNac = new JLabel(GestorIGInicio.getGestorIGInicio().getTexto(17));
			lblFecNac.setHorizontalAlignment(SwingConstants.CENTER);
		}
		return lblFecNac;
	}
	private JTextField getTxtFecNac() {
		if (txtFecNac == null) {
			txtFecNac = new JTextField();
			txtFecNac.setColumns(10);
		}
		return txtFecNac;
	}
	private JLabel getLblInvisible() {
		if (lblInvisible == null) {
			lblInvisible = new JLabel("");
		}
		return lblInvisible;
	}
	private JLabel getLblFormato() {
		if (lblFormato == null) {
			//ID-18
			lblFormato = new JLabel(GestorIGInicio.getGestorIGInicio().getTexto(18));
			lblFormato.setHorizontalAlignment(SwingConstants.CENTER);
		}
		return lblFormato;
	}
	private JLabel getLblNumCrias() {
		if (lblNumCrias == null) {
			//ID-19
			lblNumCrias = new JLabel(GestorIGInicio.getGestorIGInicio().getTexto(19));
			lblNumCrias.setHorizontalAlignment(SwingConstants.CENTER);
		}
		return lblNumCrias;
	}
	private JTextField getTxtNumCrias() {
		if (txtNumCrias == null) {
			txtNumCrias = new JTextField();
			txtNumCrias.setColumns(10);
		}
		return txtNumCrias;
	}
	private JButton getBtnAnadir() {
		if (btnAnadir == null) {
			//ID-20
			btnAnadir = new JButton(GestorIGInicio.getGestorIGInicio().getTexto(20));
			btnAnadir.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseClicked(MouseEvent e) {
					try{
						Boolean expFecha = GestorIGAnadirCamada.getGestorIGAnadirCamada().comprobarExpresionFecha(txtFecNac.getText());
						if(expFecha){
							Boolean longCrias = GestorIGAnadirCamada.getGestorIGAnadirCamada().comprobarNumCrias(txtNumCrias.getText());
							if(longCrias){
								//--Con esto damos la fecha de tipo String en tipo Date
								java.sql.Date fecFormatoDate = null;
								try {
									SimpleDateFormat sdf = new java.text.SimpleDateFormat("yyyy-MM-dd", new Locale("es", "ES"));
									fecFormatoDate = new java.sql.Date(sdf.parse(txtFecNac.getText()).getTime());
								} catch (Exception ex) {
									System.out.println("Error al obtener el formato de la fecha/hora: " + ex.getMessage());
								}
								//--
								
								Boolean existeCamada = GestorIGAnadirCamada.getGestorIGAnadirCamada().existeCamada(numPerro, fecFormatoDate);
								if(!existeCamada){
									GestorIGAnadirCamada.getGestorIGAnadirCamada().insertarCamada(numPerro, fecFormatoDate, txtNumCrias.getText());
									//ID-21 y 6
									JOptionPane.showMessageDialog(null, GestorIGInicio.getGestorIGInicio().getTexto(21), GestorIGInicio.getGestorIGInicio().getTexto(6), JOptionPane.INFORMATION_MESSAGE);
									dispose();
								}else{
									//ID-22 y 4
									JOptionPane.showMessageDialog(null, GestorIGInicio.getGestorIGInicio().getTexto(22), GestorIGInicio.getGestorIGInicio().getTexto(4), JOptionPane.WARNING_MESSAGE);
								}
							}else{
								//ID-23 y 4
								JOptionPane.showMessageDialog(null, GestorIGInicio.getGestorIGInicio().getTexto(23), GestorIGInicio.getGestorIGInicio().getTexto(4), JOptionPane.WARNING_MESSAGE);
							}
						}else{
							//ID-24 y 4
							JOptionPane.showMessageDialog(null, GestorIGInicio.getGestorIGInicio().getTexto(24), GestorIGInicio.getGestorIGInicio().getTexto(4), JOptionPane.WARNING_MESSAGE);
						}
					} catch (SQLException e1) {
						// TODO Auto-generated catch block
						e1.printStackTrace();
					}
				}
			});
		}
		return btnAnadir;
	}
	private JButton getBtnSalir() {
		if (btnSalir == null) {
			//ID-25
			btnSalir = new JButton(GestorIGInicio.getGestorIGInicio().getTexto(25));
			btnSalir.addMouseListener(new MouseAdapter() {
				@Override
				public void mouseClicked(MouseEvent arg0) {
					dispose();
				}
			});
		}
		return btnSalir;
	}
}
